package com.cheney.mapboxexample;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsControllerCompat;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.cheney.mapboxexample.mapbox.ClusterLayer;
import com.cheney.mapboxexample.mapbox.MapBoxUtil;
import com.cheney.mapboxexample.mapbox.PhotoAnnotation;
import com.mapbox.geojson.Point;
import com.mapbox.maps.MapView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MapBoxActivity extends AppCompatActivity {
    private static final String TAG = "MapBoxActivity";

    private MapView mapView;
    private MapBoxUtil mapBoxUtil;
    private boolean isMapLoadFinish = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_box);
        setSystemUIState();

        mapView = findViewById(R.id.mapview);
        mapBoxUtil = new MapBoxUtil(mapView, this, true, mOnStyleLoadFinishListener)
                .hideLogo()
                .hideScaleBar();
    }

    private final MapBoxUtil.OnStyleLoadFinishListener mOnStyleLoadFinishListener
            = new MapBoxUtil.OnStyleLoadFinishListener() {
        @SuppressLint("UseCompatLoadingForDrawables")
        @Override
        public void onFinish() {
            isMapLoadFinish = true;
            mapBoxUtil.initCompassUI(false, null, 0, 0);
            mapBoxUtil.setClusterLayerClickListener(onClusterLayerClickListener);
        }
    };

    public void addTestImage(View view) {
        if (!isMapLoadFinish) {
            Toast.makeText(this, "地图尚未加载完毕，请等待片刻后点击", Toast.LENGTH_SHORT).show();
            return;
        }
        Toast.makeText(this, "加载中, 请缩小地图以查看", Toast.LENGTH_SHORT).show();
        addImage();
    }

    public void addImage() {
        List<PhotoAnnotation> demoObjList = new ArrayList<>();
        Random random = new Random();
        // 随机添加100张图片（在中国范围内）
        for (int i = 0; i < 100; i++) {
            int lat = random.nextInt(30) + 20;
            int lng = random.nextInt(64) + 71;
            String imageName = random.nextInt(9) + ".jpg";
//            Log.d(TAG,"imageName: " + imageName);
            PhotoAnnotation demoObj = new PhotoAnnotation("" + i,
                    imageName, lat, lng);
            demoObjList.add(demoObj);
        }
        mapBoxUtil.setClusterLayerPhotoItem(demoObjList);
        // 缩小地图
        mapBoxUtil.moveCameraTo(Point.fromLngLat(103, 35), 2, 1000);
    }

    /**
     * 设置状态栏沉浸
     */
    private void setSystemUIState() {
        //沉浸
        WindowCompat.setDecorFitsSystemWindows(getWindow(), false);
        //设置状态栏和导航栏的底色，透明
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        getWindow().setNavigationBarColor(Color.TRANSPARENT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            getWindow().setNavigationBarDividerColor(Color.TRANSPARENT);
        }
        WindowInsetsControllerCompat controller = ViewCompat.getWindowInsetsController(getWindow().getDecorView());
        if (controller != null) {
            controller.setAppearanceLightStatusBars(true);
        }
    }

    private final ClusterLayer.OnClusterLayerClickListener onClusterLayerClickListener
            = clusterIdList -> {
        if (clusterIdList == null) {
            return;
        }
        Toast.makeText(this, "当前聚类包含图片数量：" + clusterIdList.size(), Toast.LENGTH_SHORT).show();
        Log.d(TAG,"[onClusterClick] Include clusterIdList size:" + clusterIdList.size());
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[onClusterClick] include image id :{ ").append(clusterIdList.get(0));
        for (int i = 1; i < clusterIdList.size(); ++i) {
            stringBuilder.append(", ").append(clusterIdList.get(i));
        }
        stringBuilder.append(" }");
        Log.d(TAG,stringBuilder.toString());
    };

    public void moveCameraToUser(View view) {
        if (mapBoxUtil != null) {
            mapBoxUtil.moveCameraToUserPos(1000);
        }
    }

    public void closeMapbox(View view) {
        this.finish();
    }


    @SuppressLint("Lifecycle")
    @Override
    protected void onStart() {
        super.onStart();
        mapView.onStart();
    }

    @SuppressLint("Lifecycle")
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }

    @SuppressLint("Lifecycle")
    @Override
    protected void onStop() {
        super.onStop();
        mapView.onStop();
    }

    @SuppressLint("Lifecycle")
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mapBoxUtil != null) {
            mapBoxUtil.onDestroy();
        }
    }

}